package cn.jtfadmin.base.lang.common.domain.entity;

import cn.jtfadmin.base.lang.config.user.UserHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 基础实体监听器过程监听
 * @author jtf
 * @since 0.0.1
 * @see CUDateUserEntity
 */
@Component
public class CUDateUserEntityListener implements EntityListener {



    /**
     * 设置id, 创建人id,创建时间,设置更新人,更新时间
     *
     * @param object 实体 必须是 CUDateUserEntity
     * @see CUDateUserEntity
     */
    public void prePersist(Object object) {
        if (object instanceof CUDateUserEntity) {
            CUDateUserEntity baseEntity = (CUDateUserEntity) object;
            if(Objects.isNull(baseEntity.getCreateDateTime())){
                baseEntity.setCreateDateTime(LocalDateTime.now());
            }
            if(Objects.isNull(baseEntity.getCreateUserId())){
                baseEntity.setCreateUserId(UserHolder.getUserId());
            }

            preUpdate(object);
        }
    }

    /**
     * 设置更新人,更新时间
     *
     * @param object 必须是 CUDateUserEntity
     * @see CUDateUserEntity
     */
    public void preUpdate(Object object) {
        if (object instanceof CUDateUserEntity) {
            CUDateUserEntity baseEntity = (CUDateUserEntity) object;
            baseEntity.setUpdateDateTime(LocalDateTime.now());
            baseEntity.setUpdateUserId(UserHolder.getUserId());
        }
    }



}
